package com.tianda.his.modules.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.system.dto.DictDataDto;
import com.tianda.his.modules.system.dto.DictTypeDto;
import com.tianda.his.modules.system.entity.SysDictData;
import com.tianda.his.modules.system.entity.SysDictType;
import com.tianda.his.modules.system.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class SysDictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询
     * @param dictDataDto
     * @return
     */
    @GetMapping("/system/dict/type/listForPage")
    public AjaxResult listPage(DictDataDto dictDataDto){
        Page<SysDictType> page = dictTypeService.listForPage(dictDataDto);


        return AjaxResult.success(page.getRecords(),page.getTotal());
    }

    /**
     * 添加字段数据
     * @param dictDataDto
     * @return
     */

    @PostMapping("/system/dict/type/addDictType")
    public AjaxResult addDictType(DictDataDto dictDataDto){
        dictTypeService.addDictType(dictDataDto);
        return AjaxResult.success();
    }

    /**
     * 根据id查询字典类型
     * @param dictTypeId
     * @return
     */
    @GetMapping("/system/dict/type/getOne/{dictTypeId}")
    public AjaxResult getOne(@PathVariable String dictTypeId){
        SysDictType dictType = dictTypeService.getById(dictTypeId);
        return AjaxResult.success(dictType);
    }

    /**
     * 查询所有的字典类型
     * @return
     */
    @GetMapping("/system/dict/type/selectAllDictType")
    public AjaxResult selectAllDictType(){
        List<SysDictType> list = dictTypeService.selectAllDictType();

        return AjaxResult.success(list);
    }

    @PutMapping("/system/dict/type/updateDictType")
    public AjaxResult updateDictType(DictTypeDto dictTypeDto){

//        SysDictType dictType = new SysDictType();
//        dictType.setDictId(dictTypeDto.getDictId());
//        dictType.setDictName(dictTypeDto.getDictName());
//        dictType.setRemark(dictTypeDto.getRemark());
//        dictType.setStatus(dictTypeDto.getStatus());
//        dictType.setDictType(dictTypeDto.getDictType());
//        dictType.setUpdateBy(dictTypeDto.getUpdateBy());
//        dictType.setUpdateTime(dictTypeDto.getUpdateTime());

        SysDictType dictType = new SysDictType();
        //将参数对象数据拷贝到数据库实体对象中（节约代码）
        //前提：两个对象的属性名相同
        BeanUtil.copyProperties(dictTypeDto, dictType,true);
        dictType.setUpdateTime(new Date());

        dictTypeService.updateById(dictType);
        return AjaxResult.success();
    }

    /**
     * 删除字典类型
     * @param dictTypeId
     * @return
     */
    @DeleteMapping("system/dict/type/deleteDictTypeByIds/{dictTypeId}")
    public AjaxResult deleteDictTypeById(@PathVariable String dictTypeId){
        dictTypeService.removeById(dictTypeId);
        return AjaxResult.success();
    }

    /**
     * 将数据库数据同步到redis中，缓存
     * @return
     */
    @GetMapping("/system/dict/type/dictCacheAsync")
    public AjaxResult dictCacheAsync(){
        dictTypeService.dictCacheAsync();
        return AjaxResult.success();
    }

}
